<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="封面图片" prop="coverPicture">
        <el-input
          v-model="queryParams.coverPicture"
          placeholder="请输入封面图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布人" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入发布人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="publishTime">
        <el-date-picker clearable
          v-model="queryParams.publishTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发布时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="置顶标志" prop="topFlag">
        <el-input
          v-model="queryParams.topFlag"
          placeholder="请输入置顶标志"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段1" prop="reVar1">
        <el-input
          v-model="queryParams.reVar1"
          placeholder="请输入扩展字段1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段2" prop="reVar2">
        <el-input
          v-model="queryParams.reVar2"
          placeholder="请输入扩展字段2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段3" prop="reVar3">
        <el-input
          v-model="queryParams.reVar3"
          placeholder="请输入扩展字段3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扩展字段4" prop="reVar4">
        <el-input
          v-model="queryParams.reVar4"
          placeholder="请输入扩展字段4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户权限" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户权限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门权限" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门权限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:video:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:video:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:video:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:video:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="自增主键" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="视频分类" align="center" prop="type" />
      <el-table-column label="地址" align="center" prop="url" />
      <el-table-column label="封面图片" align="center" prop="coverPicture" />
      <el-table-column label="说明" align="center" prop="note" />
      <el-table-column label="发布人" align="center" prop="publisher" />
      <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="置顶标志" align="center" prop="topFlag" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="扩展字段1" align="center" prop="reVar1" />
      <el-table-column label="扩展字段2" align="center" prop="reVar2" />
      <el-table-column label="扩展字段3" align="center" prop="reVar3" />
      <el-table-column label="扩展字段4" align="center" prop="reVar4" />
      <el-table-column label="用户权限" align="center" prop="userId" />
      <el-table-column label="部门权限" align="center" prop="deptId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:video:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:video:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="封面图片" prop="coverPicture">
          <el-input v-model="form.coverPicture" placeholder="请输入封面图片" />
        </el-form-item>
        <el-form-item label="说明" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="发布人" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入发布人" />
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime">
          <el-date-picker clearable
            v-model="form.publishTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="置顶标志" prop="topFlag">
          <el-input v-model="form.topFlag" placeholder="请输入置顶标志" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="扩展字段1" prop="reVar1">
          <el-input v-model="form.reVar1" placeholder="请输入扩展字段1" />
        </el-form-item>
        <el-form-item label="扩展字段2" prop="reVar2">
          <el-input v-model="form.reVar2" placeholder="请输入扩展字段2" />
        </el-form-item>
        <el-form-item label="扩展字段3" prop="reVar3">
          <el-input v-model="form.reVar3" placeholder="请输入扩展字段3" />
        </el-form-item>
        <el-form-item label="扩展字段4" prop="reVar4">
          <el-input v-model="form.reVar4" placeholder="请输入扩展字段4" />
        </el-form-item>
        <el-form-item label="用户权限" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户权限" />
        </el-form-item>
        <el-form-item label="部门权限" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门权限" />
        </el-form-item>
        <el-divider content-position="center">视频播放记录信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddOverallVideoPlaylist">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteOverallVideoPlaylist">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="overallVideoPlaylistList" :row-class-name="rowOverallVideoPlaylistIndex" @selection-change="handleOverallVideoPlaylistSelectionChange" ref="overallVideoPlaylist">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="观看人" prop="player" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.player" placeholder="请输入观看人" />
            </template>
          </el-table-column>
          <el-table-column label="观看时长" prop="playTime" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.playTime" placeholder="请输入观看时长" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVideo, getVideo, delVideo, addVideo, updateVideo } from "@/api/system/video";

export default {
  name: "Video",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedOverallVideoPlaylist: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 视频列表格数据
      videoList: [],
      // 视频播放记录表格数据
      overallVideoPlaylistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        type: null,
        url: null,
        coverPicture: null,
        note: null,
        publisher: null,
        publishTime: null,
        topFlag: null,
        reVar1: null,
        reVar2: null,
        reVar3: null,
        reVar4: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询视频列列表 */
    getList() {
      this.loading = true;
      listVideo(this.queryParams).then(response => {
        this.videoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        type: null,
        url: null,
        coverPicture: null,
        note: null,
        publisher: null,
        publishTime: null,
        topFlag: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        reVar1: null,
        reVar2: null,
        reVar3: null,
        reVar4: null,
        userId: null,
        deptId: null
      };
      this.overallVideoPlaylistList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加视频列";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVideo(id).then(response => {
        this.form = response.data;
        this.overallVideoPlaylistList = response.data.overallVideoPlaylistList;
        this.open = true;
        this.title = "修改视频列";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.overallVideoPlaylistList = this.overallVideoPlaylistList;
          if (this.form.id != null) {
            updateVideo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVideo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除视频列编号为"' + ids + '"的数据项？').then(function() {
        return delVideo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 视频播放记录序号 */
    rowOverallVideoPlaylistIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 视频播放记录添加按钮操作 */
    handleAddOverallVideoPlaylist() {
      let obj = {};
      obj.player = "";
      obj.playTime = "";
      this.overallVideoPlaylistList.push(obj);
    },
    /** 视频播放记录删除按钮操作 */
    handleDeleteOverallVideoPlaylist() {
      if (this.checkedOverallVideoPlaylist.length == 0) {
        this.$modal.msgError("请先选择要删除的视频播放记录数据");
      } else {
        const overallVideoPlaylistList = this.overallVideoPlaylistList;
        const checkedOverallVideoPlaylist = this.checkedOverallVideoPlaylist;
        this.overallVideoPlaylistList = overallVideoPlaylistList.filter(function(item) {
          return checkedOverallVideoPlaylist.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleOverallVideoPlaylistSelectionChange(selection) {
      this.checkedOverallVideoPlaylist = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/video/export', {
        ...this.queryParams
      }, `video_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
